package com.ec.system.modules.sys.controller;

import com.ec.core.common.core.api.ResponseMsg;
import com.ec.core.common.core.api.Result;
import com.ec.core.mybatis.groups.AddGroup;
import com.ec.core.mybatis.groups.DeleteGroup;
import com.ec.core.mybatis.groups.UpdateGroup;
import com.ec.core.secure.annotations.PreAuth;
import com.ec.system.modules.sys.dto.QueryMenuDTO;
import com.ec.system.modules.sys.dto.SysMenuDTO;
import com.ec.system.modules.sys.service.ISysMenuService;
import com.ec.system.modules.sys.vo.SysMenuVO;
import com.ec.system.modules.sys.vo.SysRouteVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单接口
 *
 * @author: Eric Lee
 * @time: 2021/8/6 15:13
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController {
    @Resource
    private ISysMenuService sysMenuService;

    /**
     * 菜单树形列表
     *
     * @param listMenuDTO
     * @return
     */
    @GetMapping("/list")
    @PreAuth("sys_menu_view,sys_tenant_view")
    public Result<List<SysMenuVO>> listMenu(QueryMenuDTO listMenuDTO) {
        return ResponseMsg.success(sysMenuService.listMenu(listMenuDTO));
    }


    /**
     * 获取左侧菜单路由
     *
     * @param topMenuId
     * @return
     */
    @GetMapping("/left-menu")
    public Result<SysRouteVO> querySysMenuRoutes(@RequestParam(name = "topMenuId", defaultValue = "0", required = false) Long topMenuId) {
        return ResponseMsg.success(sysMenuService.leftMenu(topMenuId));
    }

    /**
     * 查询用户拥有的菜单（不包含按钮）
     *
     * @return
     */
    @GetMapping("/userOwnedMenus")
    public Result<List<SysMenuVO>> userOwnedMenus() {
        return ResponseMsg.success(sysMenuService.userOwnedMenus());
    }

    /**
     * 查询租户拥有的菜单（包含按钮）
     *
     * @return
     */
    @GetMapping("/tenantOwnedMenus")
    public Result<List<SysMenuVO>> tenantOwnedMenus() {
        return ResponseMsg.success(sysMenuService.tenantOwnedMenus());
    }

    /**
     * 通过租户ID查询拥有的菜单（包含按钮）
     *
     * @return
     */
    @GetMapping("{tenantId}")
    @PreAuth("sys_tenant_config_per")
    public Result<List<SysMenuVO>> selectMenuByTenantId(@PathVariable Long tenantId) {
        return ResponseMsg.success(sysMenuService.selectMenuByTenantId(tenantId));
    }

    /**
     * 获取所有跳转页面URL
     *
     * @return
     */
    @GetMapping("/getAllHyperlinkMenu")
    public Result<List<SysMenuVO>> getAllHyperlinkMenu() {
        return ResponseMsg.success(sysMenuService.getAllHyperlinkMenu());
    }

    /**
     * 添加菜单
     *
     * @return
     */
    @PostMapping
    @PreAuth("sys_menu_add")
    public Result<Void> addMenu(@Validated(AddGroup.class) @RequestBody SysMenuDTO sysMenuDTO) {
        return ResponseMsg.status(sysMenuService.addMenu(sysMenuDTO));
    }

    /**
     * 更新操作
     *
     * @param sysMenuDTO
     * @return
     */
    @PutMapping
    @PreAuth("sys_menu_edit")
    public Result<Void> updateMenu(@Validated(UpdateGroup.class) @RequestBody SysMenuDTO sysMenuDTO) {
        return ResponseMsg.status(sysMenuService.updateMenu(sysMenuDTO));
    }

    /**
     * 删除操作
     *
     * @return
     */
    @DeleteMapping("{menuId}")
    @PreAuth("sys_menu_delete")
    public Result<Void> removeMenu(@Validated(DeleteGroup.class) @PathVariable long menuId) {
        SysMenuDTO sysMenuDTO = new SysMenuDTO();
        sysMenuDTO.setMenuId(menuId);
        return ResponseMsg.status(sysMenuService.removeMenu(sysMenuDTO));
    }

    /**
     * 彻底删除操作
     *
     * @return
     */
    @DeleteMapping("/delete/{menuId}")
    @PreAuth("sys_menu_delete")
    public Result<Void> deleteRemoveMenu(@Validated(DeleteGroup.class) @PathVariable long menuId) {
        return ResponseMsg.status(sysMenuService.removeMenuFromDb(menuId));
    }
}
